package io.kubesphere.devops.service;

import com.google.common.eventbus.Subscribe;
import io.kubesphere.devops.core.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
public class SimpleLogListener {

    private final MergeLogService mergeLogService;

    public SimpleLogListener(MergeLogService mergeLogService) {
        this.mergeLogService = mergeLogService;
    }

    @Subscribe
    public void doAction(final MergeEvent event) {

        DynamicDataSourceContextHolder.routeMaster();
        log.info("Record branch phase1 begin ==> userMergingDetailId:{}, " +
                "userMergingId:{}, " +
                "partitionZone:{} ",event.getUserMergingDetailId(),event.getUserMergingId(),event.getPartitionName());
        mergeLogService.mergeLog(event);
    }

    @Subscribe
    public void doAction(final MergeCommitEvent event) {
        DynamicDataSourceContextHolder.routeMaster();
        log.info("Record branch phase2 commit ==> userMergingDetailId:{} ," +
                "userMergingId:{} ",event.getUserMergingDetailId(),event.getUserMergingId());
        mergeLogService.mergingBranchCommit(event);
    }

    @Subscribe
    public void doAction(final MergeRevertEvent event) {
        DynamicDataSourceContextHolder.routeMaster();
        log.info("Record branch phase2 rollback ==> userMergingDetailId: {}," +
                "userMergingId: {} ",event.getUserMergingDetailId(),event.getUserMergingId());
        mergeLogService.mergingBranchRevert(event);
    }
}
